package hnyd.k.tang.pedometer.adapter;

import android.util.SparseArray;
import android.view.View;

/**
 * Created by tangk on 2016/10/30.
 * ViewHolder
 */
public class CommonViewHolder {

    public static <T extends View> T get(View view, int id) {
        SparseArray<View> viewHolder = (SparseArray<View>) view.getTag();
        //如果根view没有用来缓存View的集合
        if (viewHolder == null) {
            viewHolder = new SparseArray<View>();
            view.setTag(viewHolder);//创建集合和根View关联
        }
        View chidView = viewHolder.get(id);//获取根View储存在集合中的孩纸
        if (chidView == null) {//如果没有改孩纸
            //找不到该孩纸
            chidView = view.findViewById(id);
            viewHolder.put(id, chidView);//保存到集合
        }
        return (T) chidView;
    }

}
